<?php
/**
 * 微信原生支付
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/12/31
 * Time: 17:13
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\lib\payment;

use EasyWeChat\Factory;

class wxPay
{
    protected $wx_app;

    public function __construct()
    {

        $this->wx_app = Factory::payment(config('wechat.pay'));
    }

    /**
     * 下单
     * @param array $data
     * @param false $isContract
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function order(array $data = [], $isContract = false)
    {
        return $this->wx_app->order->unify($data, $isContract);
    }

    /**
     * 生成微信支付参数
     * @param string $prepayId
     * @return array|string
     */
    public function jssdk(string $prepayId = ''){
        return $this->wx_app->jssdk->bridgeConfig($prepayId);
    }

    /**
     * 商户退款
     * @param string $ordersn
     * @param string $refundNumber
     * @param int $payprice
     * @param int $refundFee
     * @param array $config
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function refund(string $ordersn,string $refundNumber,int $payprice,int $refundFee,array $config=[]){
        // 参数分别为：商户订单号、商户退款单号、订单金额、退款金额、其他参数
        return  $this->wx_app->refund->byOutTradeNumber($ordersn,$refundNumber,$payprice,$refundFee,$config);
    }


    /**
     * 微信提现
     * @param array $data
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function transfer(array $data=[]){
        return $this->wx_app->transfer->toBalance($data);
    }

}